﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using Parsowanie;
using Portfel.IS;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;

namespace Portfel.ViewModels
{
    public class MainDataViewModel : ViewModelBase
    {
        private ObservableCollection<DataItemViewModel> itemsA;
        private ObservableCollection<DataItemViewModel> itemsO;

        /// <summary>
        /// Initializes the items.
        /// </summary>
        private void InitializeItemsA()
        {
            this.itemsA = new ObservableCollection<DataItemViewModel>();
            //  var indices = HtmlSecurityParser.GetIndeces();
            var indexes = IsolatedStorageHelper.ReadFromXml<List<SecurityIndice>>(IsolatedStorageHelper.AkcjeFile);
            foreach (var s in indexes)
            {
                Brush color;
                if (s.PercentRateChange < 0)
                    color = new SolidColorBrush(Colors.Red);
                else if (s.PercentRateChange > 0)
                    color = new SolidColorBrush(Colors.Green);
                else
                    color = new SolidColorBrush(Colors.Blue);

                var gr = s.Security.SecurityName[0];
                gr = char.IsLetter(gr) ? char.ToLower(gr) : '#';
                itemsA.Add(new DataItemViewModel
                               {
                                   Color = color,
                                   ImageThumbnailSource = new Uri("Images/FrameThumbnail.png", UriKind.RelativeOrAbsolute),
                                   Title = s.Security.SecurityName,
                                   Kurs = s.Rate.ToString("#0.00"),
                                   Zmiana = s.RateChange.ToString("#0.00"),
                                   ZmianaProcent = (s.PercentRateChange / 100).ToString("+0.00%;-0.00%;0.00%"),
                                   Group = gr.ToString(),
                                   Wolumen = s.TradingAmount.ToString(),
                                   Max = s.RateMax.ToString("#0.00"),
                                   Min = s.RateMin.ToString("#0.00"),
                                   Obroty = s.Trading.ToString()
                               });
            }


        }


        private void InitializeItemsO()
        {
            this.itemsO = new ObservableCollection<DataItemViewModel>();
            //  var indices = HtmlSecurityParser.GetIndeces();
            var indexes = IsolatedStorageHelper.ReadFromXml<List<SecurityIndice>>(IsolatedStorageHelper.ObligacjeFile);
            foreach (var s in indexes)
            {
                Brush color;
                if (s.PercentRateChange < 0)
                    color = new SolidColorBrush(Colors.Red);
                else if (s.PercentRateChange > 0)
                    color = new SolidColorBrush(Colors.Green);
                else
                    color = new SolidColorBrush(Colors.Blue);

                var gr = s.Security.SecurityName[0];
                gr = char.IsLetter(gr) ? char.ToLower(gr) : '#';
                itemsO.Add(new DataItemViewModel
                {
                    Color = color,
                    ImageThumbnailSource = new Uri("Images/FrameThumbnail.png", UriKind.RelativeOrAbsolute),
                    Title = s.Security.SecurityName,
                    Kurs = s.Rate.ToString("#0.00"),
                    Zmiana = s.RateChange.ToString("#0.00"),
                    ZmianaProcent = (s.PercentRateChange / 100).ToString("+0.00%;-0.00%;0.00%"),
                    Group = gr.ToString(),
                    Termin=s.Termin,
                    Max = s.RateMax.ToString("#0.00"),
                    Min = s.RateMin.ToString("#0.00"),
                 
                });
            }


        }


        public ObservableCollection<DataItemViewModel> ItemsA
        {
            get
            {
                if (this.itemsA == null)
                {
                    this.InitializeItemsA();
                }
                return this.itemsA;
            }
            private set
            {
                this.itemsA = value;
            }
        }


        public ObservableCollection<DataItemViewModel> ItemsO
        {
            get
            {
                if (this.itemsO == null)
                {
                    this.InitializeItemsO();
                }
                return this.itemsO;
            }
            private set
            {
                this.itemsO = value;
            }
        }

        private List<string> groupPickerA;
        public List<string> GroupPickerA
        {
            get
            {
                if (groupPickerA == null)
                {
                    this.InitializeGroupPickersA();
                }
                return groupPickerA;

            }
            private set { groupPickerA = value; }
        }

        private List<string> _groupPickerO;
        public List<string> GroupPickerO
        {
            get
            {
                if (_groupPickerO == null)
                {
                    this.InitializeGroupPickersO();
                }
                return _groupPickerO;

            }
            private set { _groupPickerO = value; }
        }

        private void InitializeGroupPickersA()
        {
            groupPickerA = ItemsA.Select(i => i.Group).Distinct().ToList();
        }


        private void InitializeGroupPickersO()
        {
            _groupPickerO = ItemsO.Select(i => i.Group).Distinct().ToList();
        }

        #region GroupDescriptors
        private List<DataDescriptor> groupDescriptorsA;
        private List<DataDescriptor> groupDescriptorsO;
        /// <summary>
        /// Gets the group descriptors.
        /// </summary>
        public List<DataDescriptor> GroupDescriptorsA
        {
            get
            {
                if (this.groupDescriptorsA == null)
                {
                    this.InitializeGroupDescriptorsA();
                }
                return this.groupDescriptorsA;
            }
            private set
            {
                this.groupDescriptorsA = value;
            }
        }

        public List<DataDescriptor> GroupDescriptorsO
        {
            get
            {
                if (this.groupDescriptorsO == null)
                {
                    this.InitializeGroupDescriptorsO();
                }
                return this.groupDescriptorsO;
            }
            private set
            {
                this.groupDescriptorsO = value;
            }
        }

        /// <summary>
        /// Initializes the group descriptors that will be used for grouping in RadJumpList.
        /// </summary>
        private void InitializeGroupDescriptorsA()
        {
            this.groupDescriptorsA = new List<DataDescriptor>();
            var groupDescriptor = new GenericGroupDescriptor<DataItemViewModel, string>(item => item.Group);
            this.groupDescriptorsA.Add(groupDescriptor);
        }

        private void InitializeGroupDescriptorsO()
        {
            this.groupDescriptorsO = new List<DataDescriptor>();
            var groupDescriptor = new GenericGroupDescriptor<DataItemViewModel, string>(item => item.Group);
            this.groupDescriptorsO.Add(groupDescriptor);
        }
        #endregion
    }
}
